package com.ly.lypopupwindowdemo.fragment;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ListView;

import com.aspsine.swipetoloadlayout.OnLoadMoreListener;
import com.aspsine.swipetoloadlayout.OnRefreshListener;
import com.aspsine.swipetoloadlayout.SwipeToLoadLayout;
import com.ly.lypopupwindowdemo.R;
import com.ly.lypopupwindowdemo.adapter.LoopViewPagerAdapter;

/**
 * A simple {@link Fragment} subclass.
 */
public class RefreshStyle1 extends Fragment implements OnRefreshListener, OnLoadMoreListener {
    private SwipeToLoadLayout swipeToLoadLayout;

    private ListView listView;
    private ViewPager viewPager;
    private ViewGroup indicators;

    private LoopViewPagerAdapter mPagerAdapter;

    public RefreshStyle1() {
        // Required empty public constructor
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        return inflater.inflate(R.layout.fragment_refresh_style1, container, false);
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);

        View pagerView = LayoutInflater.from(getContext()).inflate(R.layout.layout_viewpager, listView, false);
        listView = (ListView) view.findViewById(R.id.swipe_target);
        swipeToLoadLayout = (SwipeToLoadLayout) view.findViewById(R.id.swipeToLoadLayout);
//        viewPager = (ViewPager) pagerView.findViewById(R.id.viewPager);
//        indicators = (ViewGroup) pagerView.findViewById(R.id.indicators);
//        viewPager.addOnPageChangeListener(mPagerAdapter);
//        listView.addHeaderView(pagerView);


        swipeToLoadLayout.setOnRefreshListener(this);
        swipeToLoadLayout.setOnLoadMoreListener(this);

        swipeToLoadLayout.post(new Runnable() {
            @Override
            public void run() {
                swipeToLoadLayout.setRefreshing(true);
            }
        });
    }


    @Override
    public void onRefresh() {

        swipeToLoadLayout.postDelayed(new Runnable() {
            @Override
            public void run() {
                swipeToLoadLayout.setRefreshing(false);
            }
        }, 3000);
    }

    @Override
    public void onLoadMore() {
        swipeToLoadLayout.postDelayed(new Runnable() {
            @Override
            public void run() {
                swipeToLoadLayout.setLoadingMore(false);
            }
        }, 3000);
    }
}
